package com.shouhuyun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.constant.RedisCheckConstant;
import com.shouhuyun.entity.PageResult;
import com.shouhuyun.entity.QueryPageBean;
import com.shouhuyun.entity.Result;
import com.shouhuyun.pojo.CheckItem;
import com.shouhuyun.service.CheckItemService;
import com.shouhuyun.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 检查项管理
 */

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private RedisController redisController;
    @Reference//查找服务
    private CheckItemService checkItemService;

    //新增检查项
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    public Result add(@RequestBody CheckItem checkItem){
        try{
            //从浏览器获取到checkItem对象 调用service的add方法
            checkItemService.add(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败                    MessageContant常量消息类  在common这个modul里设置了
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

        //将操作添加到redis中
        redisController.saveRedis(RedisCheckConstant.CHECKITEM_ADD);

        return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //检查项分页查询

    /**
     * 分页查询  前端返回来的数据是  QueryPageBean 对象  接受对象需要用requestBody注解
     */

    //检查项分页查询
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);

        //将操作添加到redis中
        String s = queryPageBean.getQueryString();
        if (s != null && s != "")
            redisController.saveRedis(RedisCheckConstant.CHECKITEM_QUERY);

        return pageResult;
    }

    //删除检查项
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    public Result delete(Integer id){
        try{
            checkItemService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        //将操作添加到redis中
        redisController.saveRedis(RedisCheckConstant.CHECKITEM_DELETE);
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //编辑检查项
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    public Result edit(@RequestBody CheckItem checkItem){
        try{
            checkItemService.edit(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //将操作添加到redis中
        redisController.saveRedis(RedisCheckConstant.CHECKITEM_EDIT);

        return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    //数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            CheckItem checkItem = checkItemService.findById(id);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    /**
     * 检查组
     */

    // 新增检查组的时候 查询检查项
    @RequestMapping("/findAll")
    public Result findAll(Integer id){
        try {
            //1. 调用服务层的 findAll方法
            List<CheckItem> checkItems = checkItemService.findAll();
            //1.1 成功 返回
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
